﻿/**
 * 树形表格删除功能。
 */
Ext.define('Happy.tree.action.Delete', {
    extend: 'Happy.Action',
    alias: 'action.tree-delete',
    requires: [
        'Happy.Msg',
        'Happy.Ajax'
    ],

    /**
     * @inheritdoc
     */
    defaultConfig: {
        itemId: 'delete',
        iconCls: 'delete-button',
        text: '删除',
        disabled: true,
        autoEnableAndDisable: true,
        remoteDelete: true,
        treeSelector: 'treepanel'
    },

    /**
     * @inheritdoc
     */
    onClickHandler: function (button) {
        var me = this;

        var tree = me.up(button, me.initialConfig.treeSelector);
        var record = tree.getFirstSelected();

        if (!record) {
            return;
        }

        Happy.Msg.confirmDelete(function () {
            if (me.initialConfig.remoteDelete === true) {
                me.deleteRecord(tree.getStore(), record);
            }
            else {
                tree.getStore().remove([record]);
            }
        });
    },

    /**
     * 删除记录。
     * 
     * @private
     * @param {Ext.data.TreeStore} store
     * @param {Ext.data.Model} record
     */
    deleteRecord: function (store, record) {
        Happy.Ajax.destroyTreeRecord(store, record, {
            success: function () {
                Happy.Msg.showDeleteSuccess();
            },
            failure: function (rec, operation) {
                Happy.Msg.showDeleteFailure(operation.error);
            }
        });
    }
});